package com.example.opengldemo.vertex_magical.shader;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES30;
import android.opengl.GLUtils;

import com.example.opengldemo.R;
import com.example.opengldemo.util.LoadUtil;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.TextureUtil;

public class EagleRenderer extends BaseRenderer{
    public EagleRenderer(Resources resource, String vertex, String fragment) {
        super(resource, vertex, fragment);
    }

    int operator=1;
    float span=0.15f;
    float bfbCurr=0f;
    private int markTextureId = -1;
    @Override
    protected void onCreate() {
        super.onCreate();
        shaderHandle.AHandleArray[0] = shaderHandle.getAHandle("bPosition") ;
        shaderHandle.AHandleArray[1] = shaderHandle.getAHandle("cPosition") ;
        shaderHandle.UHandleArray[0] = shaderHandle.getUHandle("uBfb") ;
        Bitmap bmp = BitmapFactory.decodeResource(mRes, R.drawable.ttt);
        if (bmp != null) {
            if (markTextureId == -1) {
                markTextureId = TextureUtil.createTextureID(false);
            } else {
                GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, markTextureId);
            }
            GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, bmp, 0);
            bmp.recycle();
        } else {
            if (markTextureId != -1) {
                GLES30.glDeleteTextures(1, new int[]{markTextureId}, 0);
            }
        }

        new Thread()
        {
            @Override
            public void run()
            {
                while(true)
                {
                    bfbCurr=bfbCurr+operator*span;
                    if(bfbCurr>2.0f)
                    {
                        bfbCurr=2.0f;
                        operator=-operator;
                    }
                    else if(bfbCurr<0)
                    {
                        bfbCurr=0;
                        operator=-operator;
                    }
                    try
                    {
                        Thread.sleep(50);
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    protected void initBuffer() {
        shaderBuffer = new ShaderBuffer() ;
        LoadUtil.getLoadObjFileToRenderer(mRes, "vertexmagical/laoying01.obj", new LoadUtil.CreateObjectRenderer() {
            @Override
            public void create(Resources res, float[] vertexArray, float[] normalArray, float[] texcoorsArray) {;
                shaderBuffer.setVertexBuffer(vertexArray);
                shaderBuffer.setTexcoorBuffer(texcoorsArray);
            }
        });
        LoadUtil.getLoadObjFileToRenderer(mRes, "vertexmagical/laoying02.obj", new LoadUtil.CreateObjectRenderer() {
            @Override
            public void create(Resources res, float[] vertexArray, float[] normalArray, float[] texcoorsArray) {
                shaderBuffer.setVertexBuffer2(vertexArray);
            }
        });
        LoadUtil.getLoadObjFileToRenderer(mRes, "vertexmagical/laoying03.obj", new LoadUtil.CreateObjectRenderer() {
            @Override
            public void create(Resources res, float[] vertexArray, float[] normalArray, float[] texcoorsArray) {
                shaderBuffer.setVertexBuffer3(vertexArray);
            }
        });

    }


    float ratio  ;
    @Override
    protected void onSizeChanged(int width, int height) {
        super.onSizeChanged(width, height);
        ratio = (float) width / height;
    }

    @Override
    protected void onClear() {
        super.onClear();
    }

    @Override
    protected void onSetExpandData() {
        super.onSetExpandData();
        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 100);
        MatrixState.setCamera(0,0, 8, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        MatrixState.pushMatrix() ;
        MatrixState.translate(0 , 0 , MatrixState.translate[2] );
        MatrixState.rotate(MatrixState.rotate[0] , 1 , 0 , 0 );
        MatrixState.rotate(MatrixState.rotate[1] , 0 , 1 , 0 );
        shaderHandle.bindMVPMaxtrixHandle() ;
        MatrixState.popMatrix() ;


        GLES30.glUniform1f(shaderHandle.UHandleArray[0] , bfbCurr);
        GLES30.glVertexAttribPointer(
                shaderHandle.AHandleArray[0] ,
                3,
                GLES30.GL_FLOAT ,
                false ,
                3*4 ,
                shaderBuffer.getVertexBuffer2()
        );
        GLES30.glVertexAttribPointer(
                shaderHandle.AHandleArray[1] ,
                3,
                GLES30.GL_FLOAT ,
                false ,
                3*4 ,
                shaderBuffer.getVertexBuffer3()
        );
        GLES30.glEnableVertexAttribArray(shaderHandle.AHandleArray[0]);
        GLES30.glEnableVertexAttribArray(shaderHandle.AHandleArray[1]);
    }


    @Override
    protected void onBindTexture(int textureId) {
        shaderHandle.bindDefaultTextureHandle(markTextureId) ;
    }
    @Override
    protected void onDraw() {
        GLES30.glEnable(GLES30.GL_BLEND);
        GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
        GLES30.glBlendEquation(GLES30.GL_FUNC_ADD);

        shaderHandle.bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer()) ;
        shaderHandle.bindVertexHandle(shaderBuffer.getVertexBuffer()) ;
        shaderHandle.run(GLES30.GL_TRIANGLES,shaderBuffer.vCount);

        GLES30.glDisable(GLES30.GL_BLEND);
    }


    @Override
    protected void onDrawEnd() {
        super.onDrawEnd();
        GLES30.glDisableVertexAttribArray(shaderHandle.AHandleArray[0]);
        GLES30.glDisableVertexAttribArray(shaderHandle.AHandleArray[1]);
    }
}
